// progressBarPlugin.js
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
import type { Router } from 'vue-router';

// const ProgressBarPlugin = {
//   install(router, options = {}) {

//   }
// };

export default function (router: Router, options = {}) {
  // 合并默认选项和用户选项
  const defaultOptions = {
    color: '#f00',
    showSpinner: false,
    speed: 200,
    minimum: 0.1,
  };

  const mergedOptions = { ...defaultOptions, ...options };

  // 配置 NProgress
  NProgress.configure(mergedOptions);

  // 全局前置守卫：开始进度条
  router.beforeEach((to, from, next) => {
    // 如果不是同一个路由才显示进度条
    if (to.fullPath !== from.fullPath) {
      NProgress.start();
    }
    next();
  });

  // 全局后置钩子：完成进度条
  router.afterEach(() => {
    NProgress.done();
  });

  // 路由错误处理
  router.onError(() => {
    NProgress.done();
  });

};